package com.realbrand.net.realbrandpay.ui.activity.wallet.order;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.adapter.OrderGroupAdapter;
import com.realbrand.net.realbrandpay.bean.AllOrderBean;
import com.realbrand.net.realbrandpay.bean.OrderBean;
import com.realbrand.net.realbrandpay.bean.OrderChildBean;
import com.realbrand.net.realbrandpay.bean.OrderFragmentBean;
import com.realbrand.net.realbrandpay.bean.OrderGroupBean;
import com.realbrand.net.realbrandpay.untils.Constant;
import com.realbrand.net.realbrandpay.untils.Static;
import com.realbrand.net.realbrandpay.zxing.util.L;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

import static android.content.Context.MODE_PRIVATE;

/**
 * Created by Administrator on 2018/4/24.
 */

public class ConfirmFragment extends Fragment implements IViewOrder {


    @BindView(R.id.OderFragment_lv)
    ListView OderFragmentLv;
    Unbinder unbinder;

    private List<OrderFragmentBean> list;

    Context mContext;
    private OrderGroupAdapter adapter;

    ProgressDialog dialog;
    PersenterOrder persenterOrder;
    String userAccount;
    //待发货list
    List<OrderFragmentBean> shipmentsList;

    //控件是否已经初始化
    private boolean isCreateView = false;

    AllOrderBean allOrderBean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.null_layout, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        this.mContext = getActivity();
        L.d("q", "confirm运行了");
        isCreateView = true;
        return inflate;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser && isCreateView) {
            initData();
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (getUserVisibleHint())
            initData();
    }

    private void initData() {
        shipmentsList = new ArrayList<>();
        dialog = new ProgressDialog(mContext);
        dialog.setCancelable(false);
        dialog.setMessage("正在加载");
        persenterOrder = new PersenterOrder();
        persenterOrder.attachView(this);
        userAccount = mContext.getSharedPreferences(Constant.shareName, MODE_PRIVATE).getString("account", "");
        JSONObject orderInfoJson = new JSONObject();
        try {
            orderInfoJson.put("METHOD", "GetOrdersInfo");
            orderInfoJson.put("SOURCE", "ManagerAdressActivity");
            orderInfoJson.put("CREDENTIALS", "0");
            orderInfoJson.put("TERMINAL", "0");
            orderInfoJson.put("INDEX", Static.getDay());
            orderInfoJson.put("UserAccount", userAccount);
            L.d(L.TAG, "所有订单信息参数" + orderInfoJson.toString());
            persenterOrder.getOrderInfoData(orderInfoJson.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void OrderInfoData(OrderBean orderBean) {
        L.d(L.TAG, "所有订单信息结果" + orderBean);
        List<OrderBean> list = new ArrayList<>();
        List<String> shipmentDealList = new ArrayList<>();
        List<String> shipmentDealNumberList = new ArrayList<>();
        List<String> shipmentDealSumList = new ArrayList<>();
        List<String> shipmentAddresseeNamemList = new ArrayList<>();
        List<String> shipmentTelephoneList = new ArrayList<>();
        List<String> shipmentProvinceList = new ArrayList<>();
        List<String> shipmentRegionCityList = new ArrayList<>();
        List<String> shipmentCountyDistrictList = new ArrayList<>();
        List<String> shipmentDetailedAddressList = new ArrayList<>();
        List<String> shipmentCarrierIdList = new ArrayList<>();
        List<String> shipmentOddNumbersList = new ArrayList<>();
        List<String> shipmentDeliveryTimeList = new ArrayList<>();
        List<String> shipmentReceiptTimeList = new ArrayList<>();
        List<String> shipmentConfirmTimeList = new ArrayList<>();
        List<String> shipmentBrandNameList = new ArrayList<>();
        List<String> shipmentCommodityNameList = new ArrayList<>();
        List<String> shipmentSupplyMoneyList = new ArrayList<>();
        List<String> shipmentBrandLogList = new ArrayList<>();
        List<String> shipmentCommodityLogList = new ArrayList<>();
        List<String> shipmentCommodityShelvesAccountList=new ArrayList<>();
        List<String> shipmentCommodityWorkAddressList=new ArrayList<>();
        List<String> shipmentCommodityDealTypetList=new ArrayList<>();
        OrderBean shipmentOrderBean = new OrderBean();
        for (int i = 0; i < orderBean.getDealNumber().size(); i++) {
            if (orderBean.getDealStatus().get(i).equals("5")) {
                shipmentDealList.add(orderBean.getDealStatus().get(i));
                shipmentDealNumberList.add(orderBean.getDealNumber().get(i));
                shipmentDealSumList.add(orderBean.getDealSum().get(i));
                shipmentAddresseeNamemList.add(orderBean.getAddresseeName().get(i));
                shipmentTelephoneList.add(orderBean.getTelephone().get(i));
                shipmentProvinceList.add(orderBean.getProvince().get(i));
                shipmentRegionCityList.add(orderBean.getRegionCity().get(i));
                shipmentCountyDistrictList.add(orderBean.getCountyDistrict().get(i));
                shipmentDetailedAddressList.add(orderBean.getDetailedAddress().get(i));
                shipmentCarrierIdList.add(orderBean.getCarrierName().get(i));
                shipmentOddNumbersList.add(orderBean.getOddNumbers().get(i));
                shipmentDeliveryTimeList.add(orderBean.getDeliveryTime().get(i));
                shipmentReceiptTimeList.add(orderBean.getReceiptTime().get(i));
                shipmentConfirmTimeList.add(orderBean.getConfirmTime().get(i));
                shipmentBrandNameList.add(orderBean.getBrandName().get(i));
                shipmentCommodityNameList.add(orderBean.getCommodityName().get(i));
                shipmentSupplyMoneyList.add(orderBean.getSupplyMoney().get(i));
                shipmentBrandLogList.add(orderBean.getBrandLogo().get(i));
                shipmentCommodityLogList.add(orderBean.getFilePath().get(i));
                shipmentCommodityShelvesAccountList.add(orderBean.getShelvesAccount().get(i));
                shipmentCommodityWorkAddressList.add(orderBean.getWorkAddress().get(i));
                shipmentCommodityDealTypetList.add(orderBean.getDealType().get(i));
            }
        }
        shipmentOrderBean.setDealStatus(shipmentDealList);
        shipmentOrderBean.setDealNumber(shipmentDealNumberList);
        shipmentOrderBean.setDealSum(shipmentDealSumList);
        shipmentOrderBean.setAddresseeName(shipmentAddresseeNamemList);
        shipmentOrderBean.setTelephone(shipmentTelephoneList);
        shipmentOrderBean.setProvince(shipmentProvinceList);
        shipmentOrderBean.setRegionCity(shipmentRegionCityList);
        shipmentOrderBean.setCountyDistrict(shipmentCountyDistrictList);
        shipmentOrderBean.setDetailedAddress(shipmentDetailedAddressList);
        shipmentOrderBean.setCarrierName(shipmentCarrierIdList);
        shipmentOrderBean.setOddNumbers(shipmentOddNumbersList);
        shipmentOrderBean.setDeliveryTime(shipmentDeliveryTimeList);
        shipmentOrderBean.setReceiptTime(shipmentReceiptTimeList);
        shipmentOrderBean.setConfirmTime(shipmentConfirmTimeList);
        shipmentOrderBean.setBrandName(shipmentBrandNameList);
        shipmentOrderBean.setCommodityName(shipmentCommodityNameList);
        shipmentOrderBean.setSupplyMoney(shipmentSupplyMoneyList);
        shipmentOrderBean.setBrandLogo(shipmentBrandLogList);
        shipmentOrderBean.setFilePath(shipmentCommodityLogList);
        shipmentOrderBean.setShelvesAccount(shipmentCommodityShelvesAccountList);
        shipmentOrderBean.setDealType(shipmentCommodityDealTypetList);
        shipmentOrderBean.setWorkAddress(shipmentCommodityWorkAddressList);
        for (int i = 0; i < shipmentOrderBean.getDealNumber().size(); i++) {
            list.add(shipmentOrderBean);
        }
        List<OrderGroupBean> init = init(list);
        allOrderBean = new AllOrderBean();
        allOrderBean.setList(init);
        L.d(L.TAG, "所有订单信息处理后的结果" + allOrderBean.toString());
        OrderGroupAdapter groupAdapter = new OrderGroupAdapter(allOrderBean, mContext);
        OderFragmentLv.setAdapter(groupAdapter);
        OderFragmentLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(mContext, OrderDeailActivity.class);
                intent.putExtra("deail", allOrderBean.getList().get(position));
                startActivity(intent);
            }
        });
    }

    @Override
    public void showLoading() {
        if (!dialog.isShowing()) {
            dialog.isShowing();
        }
    }

    @Override
    public void hideLoading() {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    public void showFailMsg(String msg) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }

    }

    @Override
    public void showError() {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private List<OrderGroupBean> init(List<OrderBean> data) {
        List<OrderGroupBean> brandBeenList = new ArrayList<OrderGroupBean>();
        for (int i = 0; i < data.get(0).getDealNumber().size(); i++) {
            OrderChildBean orderChildBean = new OrderChildBean();
            orderChildBean.setCommodityLog(data.get(0).getFilePath().get(i));
            orderChildBean.setCommodityName(data.get(0).getCommodityName().get(i));
            orderChildBean.setCommodityPrice(data.get(0).getSupplyMoney().get(i));
            orderChildBean.setCommoditySum(data.get(0).getDealSum().get(i));
            orderChildBean.setBrandName(data.get(0).getBrandName().get(i));
            orderChildBean.setDealNumber(data.get(0).getDealNumber().get(i));
            orderChildBean.setBrandLog(data.get(0).getBrandLogo().get(i));
            orderChildBean.setAddressName(data.get(0).getAddresseeName().get(i));
            orderChildBean.setTelephone(data.get(0).getTelephone().get(i));
            orderChildBean.setProvince(data.get(0).getProvince().get(i));
            orderChildBean.setRegionCity(data.get(0).getRegionCity().get(i));
            orderChildBean.setCountyDistrict(data.get(0).getCountyDistrict().get(i));
            orderChildBean.setDetailedAdress(data.get(0).getDetailedAddress().get(i));
            orderChildBean.setDeliveryTime(data.get(0).getDeliveryTime().get(i));
            orderChildBean.setReceiptTime(data.get(0).getReceiptTime().get(i));
            orderChildBean.setConfirmTime(data.get(0).getConfirmTime().get(i));
            orderChildBean.setDealStatus(data.get(0).getDealStatus().get(i));
            orderChildBean.setCarrierName(data.get(0).getCarrierName().get(i));
            orderChildBean.setOddNumber(data.get(0).getOddNumbers().get(i));
            orderChildBean.setShelvesAccount(data.get(0).getShelvesAccount().get(i));
            orderChildBean.setWorkAddress(data.get(0).getWorkAddress().get(i));
            orderChildBean.setDealType(data.get(0).getDealType().get(i));
            if (brandBeenList.size() == 0) {
                List<OrderChildBean> commodityBeanList = new ArrayList<OrderChildBean>();
                commodityBeanList.add(orderChildBean);
                OrderGroupBean orderGroupBean = new OrderGroupBean();
                orderGroupBean.setDealNumber(data.get(0).getDealNumber().get(i));
                orderGroupBean.setList(commodityBeanList);
                brandBeenList.add(orderGroupBean);
            } else {
                int j = -1;

                for (int a = 0; a < brandBeenList.size(); a++) {
                    OrderGroupBean brandBean2 = brandBeenList.get(a);
                    if (brandBean2.getDealNumber().equals(orderChildBean.getDealNumber())) {
                        brandBean2.getList().add(orderChildBean);
                        j = a;
                    }
                }
                if (j < 0) {
                    List<OrderChildBean> commodityBeanList1 = new ArrayList<OrderChildBean>();
                    commodityBeanList1.add(orderChildBean);
                    OrderGroupBean brandBea1 = new OrderGroupBean();
                    brandBea1.setDealNumber(data.get(0).getDealNumber().get(i));
                    brandBea1.setList(commodityBeanList1);
                    brandBeenList.add(brandBea1);
                }
            }
        }
        return brandBeenList;
    }
}
